package com.dayuanit.booksystem.v2.dao.impl;

import com.dayuanit.booksystem.v2.dao.PublisherDao;
import com.dayuanit.booksystem.v2.domain.PrepareParamer;
import com.dayuanit.booksystem.v2.entity.Publisher;

import java.util.ArrayList;
import java.util.List;

public class PublisherDaoImpl extends BaseDaoImpl implements PublisherDao {
    @Override
    public long add(String name, String address) {
        String sql = "insert into publisher(publisher_name, publisher_address) values(?, ?)";

        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(String.class, name));
        list.add(new PrepareParamer(String.class, address));

        //调用父类的方法 执行sql语句
        return commonAdd(sql, list);
    }

    @Override
    public Publisher getPublisherById(long id) {
        String sql = "SELECT publisher_id, publisher_name, publisher_address FROM publisher where publisher_id=?;";
        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(long.class, id));
        return commonQuery(sql, Publisher.class, publisherMap, list);
    }

    @Override
    public Publisher getPublisherByName(String name) {
        String sql = "SELECT * from publisher WHERE publisher_name=?; ";

        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(String.class, name));

        return commonQuery(sql, Publisher.class, publisherMap, list);
    }

    @Override
    public int updatePublisherAddress(long publisherId, String address) {
        String sql = "UPDATE publisher set publisher_address=? where publisher_id=?;";
        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(String.class, address));
        list.add(new PrepareParamer(long.class, publisherId));
        return modify(sql, list);
    }
}
